package sgz.ln.txt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

public class TxtDriver {
	/**
	 * 将一个Txt文件N行保留一行。
	 * 
	 * @param file
	 *            需要处理的Excel文件，处理成功的Excel文件保存在该文件名后加“result”的文件中。
	 * @param numOfUnit
	 *            隔多少行保留一行
	 * @throws IOException
	 * @throws InvalidFormatException
	 * @throws ExcelFormatException
	 * @throws TableFormatException
	 */
	public static void extractExcel(File file, int numOfUnit)
			throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(file), "gbk"));
		String[] tmpS = file.getAbsolutePath().split("\\.");
		String resultName = "";
		for (int i = 0; i < tmpS.length - 1; i++) {
			resultName += tmpS[i] + ".";
		}
		resultName += "result." + tmpS[tmpS.length - 1];
		File resultFile = new File(resultName);
		PrintStream printer = new PrintStream(resultFile, "gbk");
		int distanceToStore = numOfUnit;
		String oneLine = null;
		while ((oneLine = reader.readLine()) != null) {
			if (distanceToStore != 1) {
				distanceToStore--;
				continue;
			}
			distanceToStore = numOfUnit;
			printer.println(oneLine);
		}
		reader.close();
		printer.close();
	}
}
